package com.ceshi.util.Jenkins;

import org.testng.annotations.Test;

public class jenkinsTest {
    static final String jobXml = "<?xml version='1.1' encoding='UTF-8'?>\n" +
            "<maven2-moduleset plugin=\"maven-plugin@3.4\">\n" +
            "  <actions/>\n" +
            "  <description></description>\n" +
            "  <keepDependencies>false</keepDependencies>\n" +
            "  <properties>\n" +
            "    <com.gitee.jenkins.connection.GiteeConnectionProperty plugin=\"gitee@1.1.12\">\n" +
            "      <giteeConnection></giteeConnection>\n" +
            "    </com.gitee.jenkins.connection.GiteeConnectionProperty>\n" +
            "  </properties>\n" +
            "  <scm class=\"hudson.plugins.git.GitSCM\" plugin=\"git@4.3.0\">\n" +
            "    <configVersion>2</configVersion>\n" +
            "    <userRemoteConfigs>\n" +
            "      <hudson.plugins.git.UserRemoteConfig>\n" +
            "        <url>https://gitee.com/wangqi1995/caseunit.git</url>\n" +
            "        <credentialsId>10d5c727-e75c-45a3-8c02-1169da766744</credentialsId>\n" +
            "      </hudson.plugins.git.UserRemoteConfig>\n" +
            "    </userRemoteConfigs>\n" +
            "    <branches>\n" +
            "      <hudson.plugins.git.BranchSpec>\n" +
            "        <name>*/master</name>\n" +
            "      </hudson.plugins.git.BranchSpec>\n" +
            "    </branches>\n" +
            "    <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>\n" +
            "    <submoduleCfg class=\"list\"/>\n" +
            "    <extensions/>\n" +
            "  </scm>\n" +
            "  <canRoam>true</canRoam>\n" +
            "  <disabled>false</disabled>\n" +
            "  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>\n" +
            "  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>\n" +
            "  <triggers/>\n" +
            "  <concurrentBuild>false</concurrentBuild>\n" +
            "  <rootModule>\n" +
            "    <groupId>org.example</groupId>\n" +
            "    <artifactId>caseunit</artifactId>\n" +
            "  </rootModule>\n" +
            "  <goals>test -Dtest=controller/*</goals>\n" +
            "  <aggregatorStyleBuild>true</aggregatorStyleBuild>\n" +
            "  <incrementalBuild>false</incrementalBuild>\n" +
            "  <ignoreUpstremChanges>false</ignoreUpstremChanges>\n" +
            "  <ignoreUnsuccessfulUpstreams>false</ignoreUnsuccessfulUpstreams>\n" +
            "  <archivingDisabled>false</archivingDisabled>\n" +
            "  <siteArchivingDisabled>false</siteArchivingDisabled>\n" +
            "  <fingerprintingDisabled>false</fingerprintingDisabled>\n" +
            "  <resolveDependencies>false</resolveDependencies>\n" +
            "  <processPlugins>false</processPlugins>\n" +
            "  <mavenValidationLevel>-1</mavenValidationLevel>\n" +
            "  <runHeadless>false</runHeadless>\n" +
            "  <disableTriggerDownstreamProjects>false</disableTriggerDownstreamProjects>\n" +
            "  <blockTriggerWhenBuilding>true</blockTriggerWhenBuilding>\n" +
            "  <settings class=\"jenkins.mvn.FilePathSettingsProvider\">\n" +
            "    <path>/usr/local/maven/conf/settings.xml</path>\n" +
            "  </settings>\n" +
            "  <globalSettings class=\"jenkins.mvn.FilePathGlobalSettingsProvider\">\n" +
            "    <path>/usr/local/maven/conf/settings.xml</path>\n" +
            "  </globalSettings>\n" +
            "  <reporters/>\n" +
            "  <publishers/>\n" +
            "  <buildWrappers/>\n" +
            "  <prebuilders/>\n" +
            "  <postbuilders/>\n" +
            "  <runPostStepsIfResult>\n" +
            "    <name>FAILURE</name>\n" +
            "    <ordinal>2</ordinal>\n" +
            "    <color>RED</color>\n" +
            "    <completeBuild>true</completeBuild>\n" +
            "  </runPostStepsIfResult>\n" +
            "</maven2-moduleset>";


    static final String viewXml = "<listView _class=\\\"hudson.model.ListView\\\">\\n\" +\n" +
            "                    \"<description>用于测试的视图</description>\\n\" +\n" +
            "                    \"</listView>";

    @Test()
    public static void jobApiTest() throws Exception {
        JobApi jobApi = new JobApi();
        // 创建 Job 信息
        jobApi.ceateJob("caseunit", jobXml);
        //更新 Job
        jobApi.updateJob("caseunit", jobXml);
        //获取 Job 基本信息
        jobApi.getJob("caseunit");
        //获取 Maven Job 信息
        jobApi.getMavenJob("caseunit");
        //获取 Job 列表
        jobApi.getJobList();
        //获取 View 名称获取 Job 列表
        jobApi.getJobListByView("all");
        //查看 Job XML 信息
        jobApi.getJobConfig("caseunit");
        //执行无参数 Job build
        jobApi.buildJob("caseunit");
        //执行带参数 Job build
        jobApi.buildParamJob("caseunit");
        //停止最后构建的 Job Build
        jobApi.stopLastJobBuild("caseunit");
        //删除 Job
        jobApi.deleteJob("caseunit", true);
        //禁用 Job
        jobApi.disableJob("caseunit");
        //启用 Job
        jobApi.enableJob("caseunit", true);
    }


    @Test()
    public static void jobBuildApiTest() throws Exception {
        JobBuildApi jobBuildApi = new JobBuildApi();
        // 获取 Job 最后的 Build
        jobBuildApi.getJobLastBuild("caseunit");
        // 获取 Job 首次 Build
        jobBuildApi.getJobFirstBuild("caseunit");
        // 根据 Job Build 编号获取编译信息
        jobBuildApi.getJobByNumber("caseunit");
        // 获取 Build 全部列表
        jobBuildApi.getJobBuildListAll("caseunit");
        // 获取一定范围的 Build 列表
        jobBuildApi.getJobBuildListRange("caseunit");
        // 获取 Build 基本信息
        jobBuildApi.getJobBuildInfo("caseunit");
        // 获取 Build 详细信息
        jobBuildApi.getJobBuildDetailInfo("caseunit");
        // 获取 Build Log 日志信息
        jobBuildApi.getJobBuildLog("caseunit");
        // 获得正在执行的编译 Log 日志信息
        jobBuildApi.getBuildActiveLog("caseunit");
    }

    @Test()
    public static void viewApiTest() throws Exception {
        ViewApi viewApi = new ViewApi();
        // 创建视图
        viewApi.createView("test-view", viewXml);
        // 获取视图信息
        viewApi.getView("test-view");
        // 获取视图配置xml信息
        viewApi.getViewConfig("test-view");
        // 更新视图信息
        viewApi.updateView("test-view", viewXml);
        // 删除视图
        viewApi.deleteView("test-view");
    }

    @Test()
    public static void test() throws Exception {
        JobApi jobApi = new JobApi();
        JobBuildApi jobBuildApi = new JobBuildApi();
        ViewApi viewApi = new ViewApi();
        //查看 Job XML 信息
        jobApi.getJobConfig("caseunit");

    }


}
